package rpc.bootstrap.spring;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import rpc.conf.MethodInfo;
import rpc.core.InitClose;
import rpc.utils.ReflectionUtils;

/*********
 *
 *
 *@author 197
 *@date 2020/7/9 17:15
 *
 **/


public class RefererBean implements InitClose {

  private final Map<String, MethodInfo> methodInfos = new HashMap<>();
  private final Map<String, Method> methodMap = new HashMap<>();
  private String interfaceName;
  private Class<?> interfaceClass;
  private int timeOut = 3000;
  private String group;
  private String direct;
//  private NettyClient nettyClient;

  public String getInterfaceName() {
    return interfaceName;
  }

  public void setInterfaceName(String interfaceName) {
    this.interfaceName = interfaceName;
    interfaceClass = ReflectionUtils.getClass(this.interfaceName);
  }

  public Class<?> getInterfaceClass() {
    return interfaceClass;
  }

  public int getTimeOut() {
    return timeOut;
  }

  public void setTimeOut(int timeOut) {
    this.timeOut = timeOut;
  }

  public String getGroup() {
    return group;
  }

  public void setGroup(String group) {
    this.group = group;
  }

  public String getDirect() {
    return direct;
  }

  public RefererBean setDirect(String direct) {
    this.direct = direct;
    return this;
  }

  public Map<String, Method> getMethodMap() {
    return this.methodMap;
  }

  @Override
  public void init() {
    Method[] methods = this.interfaceClass.getMethods();
    Arrays.stream(methods).forEach(method -> {
          methodInfos.put(method.getName(), new MethodInfo(interfaceName,
              ReflectionUtils.getFliedIntValue(interfaceClass, method.getName() + "_MsgId"), method));
          methodMap.put(method.getName(), method);
        }

    );
  }

  @Override
  public void start() {

  }

  @Override
  public void close() {

  }

//  public NettyClient getNettyClient() {
//    return nettyClient;
//  }

//  public RefererBean setNettyClient(NettyClient nettyClient) {
//    this.nettyClient = nettyClient;
//    return this;
//  }
}
